package wechat.handler.work.webauth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import wechat.handler.bean.request.webauth.WebAuthAccesstokenBean;
import wechat.handler.request.webauth.IWebAuth;
import wechat.handler.request.webauth.impl.WebAuth;
import wechat.handler.util.Configure;
import wechat.handler.util.WechatUtil;

public class WebAuthHandler {
	
	private Logger log = LoggerFactory.getLogger(WebAuthHandler.class);
	
	private static Configure INS = Configure.getInstans();
	private static String SEPARATOR = "\\$\\$";
	private static String JSPAY_URL = INS.getValue("jspay");
	private static String JSSHARE_URL = INS.getValue("jsshare");
	
	
	private IWebAuth webAuth;
	
	public WebAuthHandler(){
		webAuth = new WebAuth();
	}
	
	public String getRedirectURL(String code,String state) throws Exception{
		String orderid = "";
		String type = "";
		String appid = Configure.getAppid();
		
		if (state.contains("$$")) {
			//多公众号时state以下划线分隔订单号和appid
			String[] info = state.split(SEPARATOR);
			appid = info[0];   
			orderid = info[1];
			type = info[2];
		}
		
		
		String redirectURL = INS.getValue("menu");
		
		WebAuthAccesstokenBean wAuthAccesstokenBean = null;
		String jsticket = null;
		if ("".equals(orderid)) {
			 wAuthAccesstokenBean = webAuth.getWebAccesstoken(code);
			 jsticket = webAuth.getJSTicket();
		}
		else {
			wAuthAccesstokenBean = webAuth.getWebAccesstoken(appid,code);
			jsticket = webAuth.getJSTicket(appid);
		}
		
		String noncestr = WechatUtil.getNonceStr();
		String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
		String jsSignature = "";

		
		String openid = wAuthAccesstokenBean.getOpenid();
		log.info("WebAuthHandler--getRedirectURL openid=" + openid + " jsticket=" + jsticket
				+ " noncestr=" + noncestr + " timestamp=" + timestamp  + " jsSignature=" + jsSignature);
		
		if (type.equals("pay")) {
			jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , JSPAY_URL);
			log.info("WebAuthHandler--getRedirectURL [type is pay]  JSPAY_URL=" + JSPAY_URL);
		}
		else if (state.equals("21")) {
			jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , JSSHARE_URL + openid);
			log.info("WebAuthHandler--getRedirectURL [type is 21]  JSSHARE_URL=" + JSSHARE_URL + openid);
		}
		
		JSONObject infoObj = new JSONObject();
		infoObj.put("type", state);
		infoObj.put("openId", openid);
		infoObj.put("orderId", orderid);
		infoObj.put("timestamp", timestamp);
		infoObj.put("nonceStr", noncestr);
		infoObj.put("signature", jsSignature);
		infoObj.put("appId", appid);
		
		String infoStr = JSONObject.toJSONString(infoObj);
		log.info("WebAuthHandler--getRedirectURL infoStr=" + infoStr);
		
		redirectURL = redirectURL.replace("INFO", infoStr);
		log.info("WebAuthHandler--getRedirectURL redirectURL=" + redirectURL);
		
		return redirectURL;
	}
	
	
	public String getRedirectURL(String appid,String code,String state) throws Exception{
		String orderid = "";
		String type = "";
		
		if (state.contains("$$")) {
			//多公众号时state以下划线分隔订单号和appid
			String[] info = state.split(SEPARATOR);
			//appid = info[0];   
			orderid = info[1];
			type = info[2];
		}
		
		String redirectURL = INS.getValue("menu");
		
		WebAuthAccesstokenBean wAuthAccesstokenBean = webAuth.getWebAccesstoken(appid,code);
		
		String jsticket = webAuth.getJSTicket(appid);
		String noncestr = WechatUtil.getNonceStr();
		String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
		String jsSignature = "";

		
		String openid = wAuthAccesstokenBean.getOpenid();
		log.info("WebAuthHandler--getRedirectURL openid=" + openid + " jsticket=" + jsticket
				+ " noncestr=" + noncestr + " timestamp=" + timestamp  + " jsSignature=" + jsSignature);
		
		if (type.equals("pay")) {
			jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , JSPAY_URL);
			log.info("WebAuthHandler--getRedirectURL [type is pay]  JSPAY_URL=" + JSPAY_URL);
		}
		else if (state.equals("21")) {
			jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , JSSHARE_URL + openid);
			log.info("WebAuthHandler--getRedirectURL [type is 21]  JSSHARE_URL=" + JSSHARE_URL + openid);
		}
		
		JSONObject infoObj = new JSONObject();
		infoObj.put("type", state);
		infoObj.put("openId", openid);
		infoObj.put("orderId", orderid);
		infoObj.put("timestamp", timestamp);
		infoObj.put("nonceStr", noncestr);
		infoObj.put("signature", jsSignature);
		infoObj.put("appId", appid);
		
		String infoStr = JSONObject.toJSONString(infoObj);
		log.info("WebAuthHandler--getRedirectURL infoStr=" + infoStr);
		
		redirectURL = redirectURL.replace("INFO", infoStr);
		log.info("WebAuthHandler--getRedirectURL redirectURL=" + redirectURL);
		
		return redirectURL;
	}
	
	
	public JSONObject getAuthInfo(String code,String state) throws Exception{
		String orderid = "";
		String type = "";
		String appid = Configure.getAppid();
		
		if (state.contains("$$")) {
			//多公众号时state以下划线分隔订单号和appid
			String[] info = state.split(SEPARATOR);
			appid = info[0];   
			orderid = info[1];
			type = info[2];
		}
		
		WebAuthAccesstokenBean wAuthAccesstokenBean = null;
		String jsticket = null;
		if ("".equals(orderid)) {
			 wAuthAccesstokenBean = webAuth.getWebAccesstoken(code);
			 jsticket = webAuth.getJSTicket();
		}
		else {
			wAuthAccesstokenBean = webAuth.getWebAccesstoken(appid,code);
			jsticket = webAuth.getJSTicket(appid);
		}
		
		
		String noncestr = WechatUtil.getNonceStr();
		String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
		String jsSignature = "";

		String openid = wAuthAccesstokenBean.getOpenid();
		log.info("WebAuthHandler--getAuthInfo openid=" + openid + " jsticket=" + jsticket
				+ " noncestr=" + noncestr + " timestamp=" + timestamp  + " jsSignature=" + jsSignature);
		
		if (type.equals("pay")) {
			jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , JSPAY_URL);
			log.info("WebAuthHandler--getAuthInfo [type is pay]  JSPAY_URL=" + JSPAY_URL);
		}
		else if (state.equals("21")) {
			jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , JSSHARE_URL + openid);
			log.info("WebAuthHandler--getAuthInfo [type is 21]  JSSHARE_URL=" + JSSHARE_URL + openid);
		}
		
		JSONObject infoObj = new JSONObject();
//		infoObj.put("type", state);
//		infoObj.put("orderId", orderid);
		
		infoObj.put("openId", openid);
		infoObj.put("timestamp", timestamp);
		infoObj.put("nonceStr", noncestr);
		infoObj.put("signature", jsSignature);
		infoObj.put("appId", appid);
		
//		String infoStr = JSONObject.toJSONString(infoObj);
		log.info("WebAuthHandler--getAuthInfo infoStr=" + infoObj);
		
		return infoObj;
	}
	
	
	/**
	 * 此方法是提供给授权中心,公众号服务请勿使用
	 * 
	 * @param appid
	 * @param code
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public String getAuthInfoForCentral(String appid,String code,String type) throws Exception{
		
		WebAuthAccesstokenBean wAuthAccesstokenBean = null;
		String jsticket = null;
		if (appid == null || "".equals(appid) ) {
			 wAuthAccesstokenBean = webAuth.getWebAccesstoken(code);
			 jsticket = webAuth.getJSTicket();
		}
		else {
			wAuthAccesstokenBean = webAuth.getWebAccesstoken(appid,code);
			jsticket = webAuth.getJSTicket(appid);
		}
		
		
		String noncestr = WechatUtil.getNonceStr();
		String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
		String jsSignature = "";

		String openid = wAuthAccesstokenBean.getOpenid();
		log.info("WebAuthHandler--getAuthInfoForCentral openid=" + openid + " jsticket=" + jsticket
				+ " noncestr=" + noncestr + " timestamp=" + timestamp  + " jsSignature=" + jsSignature);
		
		if (type.equals("PAY")) {
			jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , JSPAY_URL);
			log.info("WebAuthHandler--getAuthInfoForCentral [type is pay]  JSPAY_URL=" + JSPAY_URL);
		}
		else if (type.equals("SHARE")) {
			jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , JSSHARE_URL + openid);
			log.info("WebAuthHandler--getAuthInfoForCentral [type is 21]  JSSHARE_URL=" + JSSHARE_URL + openid);
		}
		
		JSONObject infoObj = new JSONObject();
		infoObj.put("openId", openid);
		infoObj.put("timestamp", timestamp);
		infoObj.put("nonceStr", noncestr);
		infoObj.put("signature", jsSignature);
		infoObj.put("appId", appid);
		
		String infoStr = JSONObject.toJSONString(infoObj);
		log.info("WebAuthHandler--getAuthInfoForCentral infoStr=" + infoStr);
		
		return infoStr;
	}
	
}
